package com.kainitram.drivebook.fragments;

import java.sql.SQLException;
import java.util.List;

import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;
import com.kainitram.drivebook.R;
import com.kainitram.drivebook.data_adapters.CarsAdapter;
import com.kainitram.drivebook.entity_activities.CarEditActivity;
import com.kainitram.drivebook.models.Car;
import com.kainitram.library.BaseArrayAdapter;
import com.kainitram.library.ButtonInfo;

/**
 * Cars fragment
 * 
 * @author Mino
 * 
 */
public class CarsListFragment extends BaseDBListFragment<Car> {

	@Override
	public String getFragmentTitle() {
		return getString(R.string.cars);
	}

	@Override
	protected List<Car> getData() {
		List<Car> list = null;
		Dao<Car, Integer> dao;
		try {
			dao = getDatabaseHelper().getCarDao();
			QueryBuilder<Car, Integer> builder = dao.queryBuilder();
			builder.orderBy(Car.EVC_FIELD_NAME, true).limit(30L);
			list = dao.query(builder.prepare());

		} catch (SQLException e) {
			e.printStackTrace();
			Log.i(Car.class.getName(), e.getMessage());
		}

		return list;
	}

	@Override
	protected BaseArrayAdapter<Car> getArrayAdapter(List<Car> data) {
		return new CarsAdapter(getActivity(), data);
	}
	
	
	@Override
	protected Class<?> getNewItemActivityClass(){
		return CarEditActivity.class;
	}

	protected ButtonInfo getNewButtonInfo() {
		ButtonInfo ret = new ButtonInfo();
		ret.setTextResId(R.string.new_car);
		ret.setImgResId(R.drawable.new_car);
		
		return ret;
	}
}
